/*
 *               ____
 *              /\   \
 *             ___\   \___
 *            /\          \
 *            \ \___    ___\
 *        ____ \/__/\   \__/
 *       /\   \    \ \___\    klof  |  innovative web technology
 *      ___\   \___ \/___/
 *     /\          \          klib3/drag.js
 *     \ \___    ___\         Create and handle draggable objects
 *      \/__/\   \__/
 *          \ \___\           Copyright 2003-2006, klof
 *           \/___/           http://www.klof.net/k.lib3/
 *
 *                            requirements: klib3/style.js
 *                                          klib3/dynamic.js
 */


/* Create the master klib3 object if she doesn't exist */
if ( typeof klib3 != "object" )
	klib3 = new ( function(){ this._child = 0 } )();

var sDragManager = "klib3_dragmanager_object";

klib3.drag = function( mElement, bNoRegister )
{
	this._version = "1.0.0";
	if ( typeof mElement != "undefined" )
		this.initDrag( mElement, bNoRegister );
};
klib3.drag.prototype = new klib3.dynamic;
klib3.drag.prototype.initDrag = function( mElement, bNoRegister )
{
	this.init( mElement );
	this._manager = window[ sDragManager ];
	if ( !bNoRegister )
		this._manager.register( this );

	this._xmouse = 0;
	this._ymouse = 0;

	this.attachEvent( "mousedown", "mouseDown" );
};
klib3.drag.prototype.getMouseOffset = function( e )
{
	this._xmouse = ( !window.event && e.pageX ? e.pageX : window.event.clientX ) - this._x;
	this._ymouse = ( !window.event && e.pageY ? e.pageY : window.event.clientY ) - this._y;
};
klib3.drag.prototype.mouseDown = function( e )
{
	this.getMouseOffset( e );
	window.status = ( this._xmouse + ", " + this._ymouse );
	this.setStyle( "border", "1px solid #f00" );
	this._manager.mouseDown( this, e );
};
klib3.drag.prototype.moveRelative = function( nX, nY )
{
	this.move( nX - this._xmouse, nY - this._ymouse );
};


klib3.dragManager = function()
{
	this._version = "1.0.0";
	this.init();
};
klib3.dragManager.prototype.init = function()
{
	this._child  = new Array();
	this._active = false;
	document.onmousemove = function( e )
	{
		window[ sDragManager ].mouseMove( e );
	}
	document.onmouseup = function( e )
	{
		window[ sDragManager ].mouseUp( e );
	}
};
klib3.dragManager.prototype.register = function( oDrag )
{
	if ( typeof oDrag != "undefined" )
	{
		var nIndex            = this._child.length;
		this._child[ nIndex ] = oDrag;
		this._child[ nIndex ].setZ( nIndex );
		return this._child[ nIndex ];
	}
	return false;
};
klib3.dragManager.prototype.unregister = function( mElement )
{
};
klib3.dragManager.prototype.mouseMove = function( e )
{
	if ( this._active )
	{
		var nX = ( window.event ? window.event.clientX : e.pageX );
		var nY = ( window.event ? window.event.clientY : e.pageY );
		this._active.moveRelative( nX, nY );
	}
};
klib3.dragManager.prototype.mouseDown = function( oDrag, e )
{
	if ( typeof oDrag == "object" )
	{
		this.zOrder( this.getIndex( oDrag ) );
		this._active = oDrag;
	}
};
klib3.dragManager.prototype.mouseUp = function( e )
{
	this._active = false;
};
klib3.dragManager.prototype.getIndex = function( oChild )
{
	for ( var i = 0; i < this._child.length; ++i )
		if ( this._child[ i ] == oChild )
			break;
	return i;
};
klib3.dragManager.prototype.zOrder = function( nIndex )
{
	window.status = ( "ordering by index: " + nIndex );
	for ( var i = 0; i < this._child.length; ++i )
		if ( this._child[ i ]._z >= this._child[ nIndex ]._z )
			this._child[ i ].setZ( this._child[ i ]._z - 1 );
	this._child[ nIndex ].setZ( i );
};


if ( typeof window[ sDragManager ] != "object" )
	window[ sDragManager ] = new klib3.dragManager();
